<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>本地视频播放器</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #000;
            color: #fff;
            font-family: Arial, sans-serif;
            width: 100vw;
            height: 100vh;
            overflow: hidden;
        }
        .container {
            padding: 10px;
            height: 100%;
            display: flex;
            flex-direction: column;
        }
        .video-container {
            flex: 1;
            width: 100%;
            margin-bottom: 15px;
        }
        #video {
            width: 100%;
            height: 100%;
            background: #000;
            border-radius: 8px;
        }
        .controls {
            margin-bottom: 15px;
            flex-shrink: 0;
            text-align: center;
        }
        button {
            background: #007cba;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            min-width: 120px;
        }
        button:hover {
            background: #005a8b;
        }
        .status {
            padding: 12px;
            background: #333;
            border-radius: 6px;
            font-size: 14px;
            flex-shrink: 0;
            text-align: center;
            max-height: 80px;
            overflow-y: auto;
        }
        .success {
            background: #4CAF50;
        }
        .error {
            background: #f44336;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="video-container">
            <video id="video" controls>
                您的浏览器不支持视频播放
            </video>
        </div>
        
        <div class="controls">
            <button onclick="playLocalFile()">播放视频</button>
        </div>
        
        <div class="status" id="status">
            等待播放视频...
        </div>
    </div>

    <!-- 引入播放器库 -->
    <script src="./hls.js"></script>
    
    <script>
        var video = document.getElementById('video');
        var status = document.getElementById('status');
        var currentPlayer = null;
        
        function updateStatus(msg, type = '') {
            status.innerHTML = msg;
            status.className = 'status ' + type;
            console.log(msg);
        }
        
        function destroyCurrentPlayer() {
            if (currentPlayer) {
                try {
                    if (currentPlayer.destroy) {
                        currentPlayer.destroy();
                    }
                } catch (e) {
                    console.log('销毁播放器时出错:', e);
                }
                currentPlayer = null;
            }
        }
        
        // 播放m3u8 文件
        function playLocalFile() {
            destroyCurrentPlayer();
            updateStatus('正在加载视频流...');
            
            // 更改为远程HLS流地址
            var localUrl = 'http://115.175.45.50:80/hls/stream.m3u8';
            
            
            if (typeof Hls !== 'undefined' && Hls.isSupported()) {
                var hls = new Hls({
                    debug: false,
                    enableWorker: false,
                    xhrSetup: function(xhr, url) {
                        xhr.timeout = 10000;
                    }
                });
                
                hls.loadSource(localUrl);
                hls.attachMedia(video);
                
                hls.on(Hls.Events.MANIFEST_PARSED, function () {
                    updateStatus('✅ 视频加载成功，正在播放...', 'success');
                    video.play().catch(e => {
                        updateStatus('播放失败: ' + e.message, 'error');
                    });
                });
                
                hls.on(Hls.Events.ERROR, function (event, data) {
                    updateStatus('❌ 播放错误: ' + (data.details || data.type), 'error');
                    console.error('HLS Error:', data);
                });
                
                hls.on(Hls.Events.FRAG_LOADED, function () {
                    updateStatus('✅ 视频正在播放中...', 'success');
                });
                
                currentPlayer = hls;
                
            } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = localUrl;
                video.play().catch(e => {
                    updateStatus('播放失败: ' + e.message, 'error');
                });
                updateStatus('✅ 使用原生播放器播放', 'success');
                
            } else {
                var hlsStatus = typeof Hls !== 'undefined' ? '已加载' : '未加载';
                updateStatus('❌ 不支持视频播放，HLS库: ' + hlsStatus, 'error');
            }
        }
        
        // 页面加载完成后自动播放
        window.onload = function() {
            updateStatus('页面加载完成，准备播放本地视频...');
            
            // 检查HLS库状态
            if (typeof Hls !== 'undefined') {
                updateStatus('HLS库加载成功，准备播放...');
                setTimeout(playLocalFile, 1000);
            } else {
                updateStatus('❌ HLS库加载失败', 'error');
            }
        };
        
        // 视频事件监听
        video.addEventListener('loadstart', function() {
            updateStatus('开始加载视频...');
        });
        
        video.addEventListener('canplay', function() {
            updateStatus('✅ 视频可以播放', 'success');
        });
        
        video.addEventListener('playing', function() {
            updateStatus('✅ 视频正在播放', 'success');
        });
        
        video.addEventListener('pause', function() {
            updateStatus('视频已暂停');
        });
        
        video.addEventListener('ended', function() {
            updateStatus('视频播放完成');
        });
        
        video.addEventListener('error', function(e) {
            updateStatus('❌ 视频播放出错', 'error');
        });
    </script>
</body>
</html> 